package com.tanhua.manage.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.manage.exception.BusinessException;
import com.tanhua.manage.mapper.UserFreezeMapper;
import com.tanhua.manage.pojo.UserFreeze;
import com.tanhua.manage.vo.UserFreezeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
public class UserFreezeService extends ServiceImpl<UserFreezeMapper,UserFreeze> {

    private static final String CACHE_KEY_FREEZE_PREFIX="FREEZE_";
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 获取用户的冻结状态
     */
    public Boolean getFreezeStatusByUserId(Long userId){
        String cacheKey=CACHE_KEY_FREEZE_PREFIX + userId;
        return redisTemplate.hasKey(cacheKey);
    }

    /**
     * 冻结用户操作
     * @param userFreezeVo
     * @return
     */
    public Boolean freeze(UserFreezeVo userFreezeVo){

        //前端传过来一个userFreezeVo对象,判断用户id是否为空
        if(ObjectUtil.isEmpty(userFreezeVo.getUserId())){
            throw new BusinessException("用户id不能为空!");
        }

        //查询数据库tb_user_freeze表,判断之前是否已经被冻结
        QueryWrapper<UserFreeze> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userFreezeVo.getUserId());
        UserFreeze userFreeze = this.getOne(queryWrapper);
        if(ObjectUtil.isNotEmpty(userFreeze)){
            throw new BusinessException("该用户不能重复冻结!");
        }

        //如果未被冻结,将用户传过来的数据即userFreezeVo的数据封装到userFreeze对象
        UserFreeze userFreezeSave = BeanUtil.toBean(userFreezeVo, UserFreeze.class);

        //保存冻结状态到数据库的tb_user_freeze表
        this.save(userFreezeSave);

        //将用户的冻结状态保存到redis中
        //day如果是0代表的是永久冻结
        int days=0;
        if(userFreezeVo.getFreezingTime()==1){
            //冻结三天
            days=3;
        }else if(userFreezeVo.getFreezingTime()==2){
            //冻结七天
            days=7;
        }

        //还要把用户的冻结状态同步保存到redis
        String cacheKey=CACHE_KEY_FREEZE_PREFIX+userFreezeVo.getUserId();

        if(days>0){
            //设置redis中的key的有效时间,过了有效时间key会自动失效,也就自动解冻了
            this.redisTemplate.opsForValue().set(cacheKey,"ok", Duration.ofDays(days));
        }else {
            //永久冻结不需要设置key的有效时间
            this.redisTemplate.opsForValue().set(cacheKey,"ok");
        }

        return true;

    }

    /**
     * 用户解冻操作
     * @param userFreezeVo
     * @return
     */
    public Boolean unfreeze(UserFreezeVo userFreezeVo){
        if(ObjectUtil.isEmpty(userFreezeVo.getUserId())){
            throw new BusinessException("用户id不能为空!");
        }

        //判断之前是否已经被冻结
        QueryWrapper<UserFreeze> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userFreezeVo.getUserId());
        UserFreeze userFreeze = this.getOne(queryWrapper);
        //如果数据库中没有该用户说明该用户未被冻结
        if(ObjectUtil.isEmpty(userFreeze)){
            throw new BusinessException("该用户未被冻结!");
        }
        //如果数据库中有该用户,则删除数据库中的该用户数据
        boolean remove = this.remove(queryWrapper);

        //再删除redis中的数据
        String cacheKey=CACHE_KEY_FREEZE_PREFIX + userFreezeVo.getUserId();
        this.redisTemplate.delete(cacheKey);

        return true;

    }

}
